//index.js
//获取应用实例
var util = require('../../utils/util.js');
var app = getApp();
var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 7))).split(' ')[0];
var minday = util.formatTime(new Date()).split(' ')[0];
var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var page = 1;
var list = new Array();
var list1 = new Array();
var list2 = new Array();

Page({
  data: {
    tabs: ["全部","车主", "乘客"],
    activeIndex: 0,
    today:today,
    minday:minday,
    maxday:maxday
  },
  tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    bindDateChange:function(e){
        this.setData({
            today:e.detail.value
        })
        this.getList(e.detail.value,this.data.start,this.data.end);
    },
    getstart:function(e){
        this.setData({
            start:e.detail.value
        })
        page = 1;
        this.getList(this.data.today,e.detail.value,this.data.end);
    },
    getend:function(e){
        this.setData({
            end:e.detail.value
        })
        page = 1;
        this.getList(this.data.today,this.data.start,e.detail.value);
    },
  onShareAppMessage: function () {
    return {
      title: '行程',
      path: 'pages/index/index'
    }
  },
  getList: function (today='',start='',end='') {
    var that = this;
    util.req('/wx/wxOrder/list',
      {page:page,today:today,start:start,end:end},
      function (data) {
        if (data.content == null || data.content.length == 0) {
          that.setData({ nomore: true });
          return false;
        }
        if (page == 1) {
          list = new Array();
          list1 = new Array();
          list2 = new Array();
        }
        var surp = new Array('', '空位', '人');
        data.content.forEach(function (item) {
          var obj = {
            start: item.departure,
            over: item.destination,
            type: that.data.tabs[item.type],
            tp: item.type,
            time: item.time,
            avatarUrl: app.globalData.userInfo.avatarUrl,
            url: '/pages/info/index?id=' + item.id,
            tm: util.getDateDiff(item.time * 1000)
          };
          if (item.type == 1) {
            obj.surplus=item.registrationDate + surp[item.type]
            list1.push(obj);
          } else {
            obj.surplus=item.number + surp[item.type]
            list2.push(obj);
          }
          list.push(obj);
        })

        that.setData({ list: list, list1: list1, list2: list2 });
        console.log(that.data)
      })

  },
  onPullDownRefresh: function () {
    page = 1;
    this.getList();
    wx.stopPullDownRefresh();
  },
  onLoad: function () {
    var that = this;
    util.req('/wx/index/getImg', {}, function (data) {
      that.setData({ movies: data.content });
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        });
      }
    });

    wx.getLocation({
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.request({
          url: 'https://api.map.baidu.com/geocoder/v2/?ak=zIOkoO8wWrWA22ObIHPNkCgtLZpkP5lE&location=' + latitude + ',' + longitude + '&output=json&pois=0',
          data: {},
          method: 'GET',
          header: { 'Content-Type': 'application/json' },
          success: function (res) {
            console.log(res)
            that.setData({
              start: res.data.result.addressComponent.city
            })
            that.getList();
          }
        })
      }
    })
  },
  sexDeparture:function(){
    var that = this;
    wx.chooseLocation({
      success:function(res){
        that.setData({
          departure:res.address
        })
      }
    })
  },
  sexDestination:function(){
    var that = this;
    wx.chooseLocation({
      success:function(res){
        that.setData({
          destination:res.address
        })
      }
    })
  },
  jump:function(){
    debugger
    //0车主，1乘客
    var a = this.data.activeIndex
    //0市内，1跨城
    
    //出发地
    var start = this.data.departure
    //目的地
    var end = this.data.destination
    wx.redirectTo({ 
       url: '/pages/info/add?activeIndex='+a+'&start='+start+'&end='+end
      })
  },
  onReachBottom: function () {
    if (!this.data.nomore) {
      page++;
      this.getList();
    }
  }
})
